package pub.cleangao.codegen.enquiry.mysql;

import pub.cleangao.codegen.GenConfigHolder;
import pub.cleangao.codegen.enquiry.ColumnEnquiry;
import pub.cleangao.codegen.enquiry.TableEnquiry;
import pub.cleangao.codegen.enquiry.TableInfo;
import pub.cleangao.codegen.util.ClassField;
import java.util.Map;


public class MySQLTableEnquiry extends TableEnquiry {


    public MySQLTableEnquiry(GenConfigHolder config, ColumnEnquiry columnEnquiry) {
        super(config, columnEnquiry);
    }


    @Override
    public String getShowTablesSql() {
        String dbName = config.getDbName();
        if (!(dbName.startsWith("`") && dbName.endsWith("`"))) {
            dbName = String.format("`%s`", dbName);
        }
        String sql = "SHOW TABLE STATUS FROM " + dbName;
        if (this.schemaTables != null && this.schemaTables.size() > 0) {
            StringBuilder tables = new StringBuilder();
            for (String table : schemaTables) {
                tables.append(",'").append(table).append("'");
            }
            sql += " WHERE NAME IN (" + tables.substring(1) + ")";
        }
        return sql;
    }

    @Override
    public TableInfo buildTableInfo(Map<String, Object> tableMap) {
        TableInfo tableInfo = new TableInfo();
        tableInfo.setTableName(ClassField.convertString(tableMap.get("NAME")));
        tableInfo.setTableNameHyphen(tableInfo.getTableName().replace("_", "-"));
        tableInfo.setComment(ClassField.convertString(tableMap.get("COMMENT")));
        return tableInfo;
    }
}
